//
//  AppDelegate.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "TypeViewController.h"
#import "FoundViewController.h"
#import "UserViewController.h"
#import "FirstLaunchViewController.h"
#import "UMSocial.h"


@interface AppDelegate () <UIScrollViewDelegate>

@property (nonatomic, retain) UINavigationController *homeNC;
@property (nonatomic, assign) BOOL isOut;

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    sleep(1);
    
    self.isOut = NO;
    
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
    
    // 是否第一次启动
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"]) {
        
        //设置默认城市
        [[NSUserDefaults standardUserDefaults] setObject:@"北京市" forKey:@"cityName"];
        [[NSUserDefaults standardUserDefaults] setObject:@"110000" forKey:@"cityid"];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isRecord"];
        
        [self makeLaunchView];
        
    } else {
        [self gotoMain];
    }
    
    return YES;
}

//假引导页面
-(void)makeLaunchView{
    
    FirstLaunchViewController *firsVC = [[FirstLaunchViewController alloc] init];
    self.window.rootViewController = firsVC;
    
    NSArray *arr = [NSArray arrayWithObjects:@"launch-1.png",@"launch-2.png",@"launch-3.png", nil];//数组内存放的是我要显示的假引导页面图片
    //通过scrollView 将这些图片添加在上面，从而达到滚动这些图片
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    scr.backgroundColor = [UIColor clearColor];
    scr.contentSize = CGSizeMake(SCREEN_WIDTH * arr.count, self.window.frame.size.height);
    scr.pagingEnabled = YES;
    scr.tag = 7000;
    scr.delegate = self;
    [self.window addSubview:scr];
    for (int i = 0; i < arr.count; i++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, self.window.frame.size.height)];
        img.image = [UIImage imageNamed:arr[i]];
        [scr addSubview:img];
        [img release];
    }
}
#pragma mark scrollView的代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //当图片全部滑完后 又像后多滑了10 的时候就做下一个动作
    if (scrollView.contentOffset.x > 2 * SCREEN_WIDTH + 10) {
        
        self.isOut=YES;//这是我声明的一个全局变量Bool 类型的，初始值为no，当达到我需求的条件时将值改为yes
    }
}
//停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //判断isout为真 就要进入主界面了
    if (self.isOut) {
        //添加一个动画
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha=0;//让scrollview 渐变消失
            
        }completion:^(BOOL finished) {
//            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirst"];
            
            [scrollView  removeFromSuperview];//将scrollView移除
            [self gotoMain];
        } ];
    }
    
}
-(void)gotoMain
{
    // 判断夜间模式
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isNight"] isEqualToString:@"1"]) {
        [DKNightVersionManager nightFalling];
    } else {
        [DKNightVersionManager dawnComing];
    }
    
    
    // 打开数据库
    [[Singleton shareSingleton] openDB];
    // 创建选择分类表
    [[Singleton shareSingleton] exeWithSQL:@"CREATE TABLE IF NOT EXISTS chooseType(number INTEGER PRIMARY KEY AUTOINCREMENT, Id TEXT, isTrue TEXT, name TEXT)"];
    // 创建浏览记录表
    [[Singleton shareSingleton] exeWithSQL:@"CREATE TABLE IF NOT EXISTS Record(number INTEGER PRIMARY KEY AUTOINCREMENT, Id TEXT, time TEXT, title TEXT)"];
    // 创建收藏记录表
    [[Singleton shareSingleton] exeWithSQL:@"CREATE TABLE IF NOT EXISTS storeUp(number INTEGER PRIMARY KEY AUTOINCREMENT, Id TEXT, time TEXT, title TEXT)"];
    
    
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    
    
    FirstLaunchViewController *firstVC = [[FirstLaunchViewController alloc] init];
    firstVC.tabBarController.hidesBottomBarWhenPushed = YES;
    self.homeNC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    self.homeNC.navigationBar.translucent = NO;
    self.homeNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[[UIImage imageNamed:@"iconfont-home-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    

    
    
    TypeViewController *typeVC = [[TypeViewController alloc] init];
    UINavigationController *typeNC = [[UINavigationController alloc] initWithRootViewController:typeVC];
    typeNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:[[UIImage imageNamed:@"iconfont-fenlei-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-fenlei.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    FoundViewController *foundVC = [[FoundViewController alloc] init];
    UINavigationController *foundNC = [[UINavigationController alloc] initWithRootViewController:foundVC];
    foundNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[[UIImage imageNamed:@"iconfont-find-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-find.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    UserViewController *userVC = [[UserViewController alloc] init];
    UINavigationController *userNC = [[UINavigationController alloc] initWithRootViewController:userVC];

    userNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[[UIImage imageNamed:@"iconfont-yonghu-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-yonghu.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    
    UITabBarController *tabBC = [[UITabBarController alloc] init];
    tabBC.tabBar.translucent = NO;
    tabBC.tabBar.tintColor = [UIColor colorWithRed:0.86 green:0.34 blue:0.18 alpha:1];
    
    tabBC.viewControllers = [NSArray arrayWithObjects:self.homeNC, typeNC, foundNC, userNC, nil];
    
    
    
    self.window.rootViewController = tabBC;
    [tabBC release];
    
    
    [homeVC release];
    [_homeNC release];
    
    [typeVC release];
    [typeNC release];
    
    [foundVC release];
    [foundNC release];
    
    [userVC release];
    [userNC release];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
